package com.deceive.demo;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class JudgeUtils {
    //判断设备是否 rooted
    public static boolean isRooted() {
        String su = "su";
        String[] locations = new String[]{"/system/bin/", "/system/xbin/", "/sbin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/", "/system/sbin/", "/usr/bin/", "/vendor/bin/"};
        String[] var2 = locations;
        int var3 = locations.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String location = var2[var4];
            if ((new File(location + su)).exists()) {
                return true;
            }
        }

        return false;
    }


    // 判断是不是模拟器
    public static boolean isSimulator() {
        boolean checkProperty = Build.FINGERPRINT.startsWith("generic") || Build.FINGERPRINT.toLowerCase().contains("vbox") || Build.FINGERPRINT.toLowerCase().contains("test-keys") || Build.MODEL.contains("google_sdk") || Build.MODEL.contains("Emulator") || Build.MODEL.contains("Android SDK built for x86") || Build.MANUFACTURER.contains("Genymotion") || Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic") || "google_sdk".equals(Build.PRODUCT);
        if (checkProperty) {
            return true;
        } else {
            String operatorName = "";
            TelephonyManager tm = (TelephonyManager) MyApp.getApp().getSystemService(Context.TELEPHONY_SERVICE);
            if (tm != null) {
                String name = tm.getNetworkOperatorName();
                if (name != null) {
                    operatorName = name;
                }
            }

            boolean checkOperatorName = operatorName.toLowerCase().equals("android");
            if (checkOperatorName) {
                return true;
            } else {
                String url = "tel:123456";
                Intent intent = new Intent();
                intent.setData(Uri.parse(url));
                intent.setAction("android.intent.action.DIAL");
                boolean checkDial = intent.resolveActivity(MyApp.getApp().getPackageManager()) == null;
                if (checkDial) {
                    return true;
                } else {
                    return isEmulatorByCpu();
                }
            }
        }
    }

    private static boolean isEmulatorByCpu() {
        String cpuInfo = readCpuInfo();
        return cpuInfo.contains("intel") || cpuInfo.contains("amd");
    }

    private static String readCpuInfo() {
        String result = "";

        try {
            String[] args = new String[]{"/system/bin/cat", "/proc/cpuinfo"};
            ProcessBuilder cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            StringBuilder sb = new StringBuilder();
            BufferedReader responseReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "utf-8"));

            String readLine;
            while((readLine = responseReader.readLine()) != null) {
                sb.append(readLine);
            }

            responseReader.close();
            result = sb.toString().toLowerCase();
        } catch (IOException var7) {
        }

        return result;
    }
}
